﻿using System;
using UnityEngine;

public class GameController : MonoBehaviour
{
    private GameObject tilePrefab;
    public enum ChessInfo { Blank, Black, White }
    private ChessInfo chessInfo;
    private ChessInfo[,] snapshot;
    private Vector2 mousePosition;
    public enum Turn { Black, White }
    private Turn turn = Turn.Black;
    private GameObject blackChess;
    private GameObject whiteChess;
    private int mousePositionX;
    private int mousePositionY;
    private int count;
    private void Awake()
    {
        Assignment();
        InitChessBoard();
    }
    private void Update()
    {
        PutChess();
    }
    private void Assignment()
    {
        snapshot = new ChessInfo[15, 15];
        tilePrefab = Resources.Load<GameObject>("Prefabs/Tile");
        blackChess = Resources.Load<GameObject>("Prefabs/BlackChess");
        whiteChess = Resources.Load<GameObject>("Prefabs/WhiteChess");
    }
    private void InitChessBoard()
    {
        for (int row = -7; row < 7; row++)
        {
            for (int col = -7; col < 7; col++)
            {
                Instantiate(tilePrefab, new Vector2(row + 0.5f, col + 0.5f), Quaternion.identity, transform).name = $"{row}_{col}";
            }
        }
    }
    private void PutChess()
    {
        if (Input.GetMouseButtonDown(0))
        {
            mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            if (mousePosition.x < 0)
            {
                mousePositionX = (int)(mousePosition.x - 0.5f);
            }
            else
            {
                mousePositionX = (int)(mousePosition.x + 0.5f);
            }
            if (mousePosition.y < 0)
            {
                mousePositionY = (int)(mousePosition.y - 0.5f);
            }
            else
            {
                mousePositionY = (int)(mousePosition.y + 0.5);
            }
            if (mousePositionX <= 7 && mousePositionX >= -7 && mousePositionY <= 7 && mousePositionY >= -7)
            {
                CreateChess(mousePositionX, mousePositionY);
            }
        }
    }
    private void CreateChess(int row, int col)
    {
        var rowIndex = row + 7;
        var colIndex = col + 7;
        if (snapshot[rowIndex, colIndex] == ChessInfo.Blank)
        {
            snapshot[rowIndex, colIndex] = turn == Turn.Black ? ChessInfo.Black : ChessInfo.White;
            switch (turn)
            {
                case Turn.Black:
                    Instantiate(blackChess, new Vector2(row, col), Quaternion.identity).name = blackChess.name + "_" + row + "_" + -col;
                    DetectWiner(rowIndex, colIndex);
                    turn = Turn.White;
                    break;
                case Turn.White:
                    Instantiate(whiteChess, new Vector2(row, col), Quaternion.identity).name = whiteChess.name + "_" + row + "_" + -col;
                    DetectWiner(rowIndex, colIndex);
                    turn = Turn.Black;
                    break;
            }
        }
    }
    private void DetectWiner(int rowIndex, int colIndex)
    {
        switch (turn)
        {
            case Turn.Black:
                if (HorizontalDetect(rowIndex, colIndex, ChessInfo.Black) || verticalDetect(rowIndex, colIndex, ChessInfo.Black)
                 || SlashDetect(rowIndex, colIndex, ChessInfo.Black) || BackSlashDetect(rowIndex, colIndex, ChessInfo.Black))
                {
                    Debug.Log("BlackWin");
                }
                break;
            case Turn.White:
                if (HorizontalDetect(rowIndex, colIndex, ChessInfo.White) || verticalDetect(rowIndex, colIndex, ChessInfo.White)
                 || SlashDetect(rowIndex, colIndex, ChessInfo.White) || BackSlashDetect(rowIndex, colIndex, ChessInfo.White))
                {
                    Debug.Log("WhiteWin");
                }
                break;
        }
    }
    private bool HorizontalDetect(int rowIndex, int colIndex, ChessInfo chessInfo)
    {
        count = 0;
        int positiveRowIndex = rowIndex;
        while (positiveRowIndex <= 14 && snapshot[positiveRowIndex, colIndex] == chessInfo)
        {
            count++;
            positiveRowIndex++;
        }
        int negativeRowIndex = rowIndex;
        while (negativeRowIndex >= 0 && snapshot[negativeRowIndex, colIndex] == chessInfo)
        {
            count++;
            negativeRowIndex--;
        }
        return count >= 6;
    }
    private bool verticalDetect(int rowIndex, int colIndex, ChessInfo chessInfo)
    {
        count = 0;
        int positiveColIndex = colIndex;
        while (positiveColIndex <= 14 && snapshot[rowIndex, positiveColIndex] == chessInfo)
        {
            count++;
            positiveColIndex++;
        }
        int negativeColIndex = colIndex;
        while (negativeColIndex >= 0 && snapshot[rowIndex, negativeColIndex] == chessInfo)
        {
            count++;
            negativeColIndex--;
        }
        return count >= 6;
    }
    private bool SlashDetect(int rowIndex, int colIndex, ChessInfo chessInfo)
    {
        count = 0;
        int positiveRowIndex = rowIndex;
        int positiveColIndex = colIndex;
        while (positiveRowIndex <= 14 && positiveColIndex <= 14 && snapshot[positiveRowIndex, positiveColIndex] == chessInfo)
        {
            count++;
            positiveRowIndex++;
            positiveColIndex++;
        }
        int negativeRowIndex = rowIndex;
        int negativeColIndex = colIndex;
        while (negativeRowIndex >= 0 && negativeColIndex >= 0 && snapshot[negativeRowIndex, negativeColIndex] == chessInfo)
        {
            count++;
            negativeRowIndex--;
            negativeColIndex--;
        }
        return count >= 6;
    }
    private bool BackSlashDetect(int rowIndex, int colIndex, ChessInfo chessInfo)
    {
        count = 0;
        int positiveRowIndex = rowIndex;
        int negativeColIndex = colIndex;
        while (positiveRowIndex <= 14 && negativeColIndex >= 0 && snapshot[positiveRowIndex, negativeColIndex] == chessInfo)
        {
            count++;
            positiveRowIndex++;
            negativeColIndex--;
        }
        int negativeRowIndex = rowIndex;
        int positiveColIndex = colIndex;
        while (negativeRowIndex >= 0 && positiveColIndex <= 14 && snapshot[negativeRowIndex, positiveColIndex] == chessInfo)
        {
            count++;
            negativeRowIndex--;
            positiveColIndex++;
        }
        return count >= 6;
    }
}